var location_map = null;
var g_marker = null;
var g_geocoder = null;
function initializeLocationMap(country) {
	var geoLat = geo_default_lat;
	var geoLng = geo_default_lng;
	var level = g_default_level;
	if(gps_map && gps_map[g_seluin] && gps_map[g_seluin].lat){
		geoLat = parseFloat(gps_map[g_seluin].lat);
		geoLng = parseFloat(gps_map[g_seluin].lng);
		level = focusLevel;
		setLocationLabelForTracker(gps_map[g_seluin].lat, gps_map[g_seluin].lng);
	}
if(country=='malaysia')
	location_map = new GPS_Map("locationMapDiv", geoLat, geoLng, level, g_default_type, 'geoserver', country /*malaysia*/);
else if(country=='nigeria')
	location_map = new GPS_Map("locationMapDiv", geoLat, geoLng, level, g_default_type, 'geoserver', country /*malaysia*/);
else if(country=='kenya')
	location_map = new GPS_Map("locationMapDiv", geoLat, geoLng, level, g_default_type, 'geoserver', country /*malaysia*/);
else if(country=='tanzania')
	location_map = new GPS_Map("locationMapDiv", geoLat, geoLng, level, g_default_type, 'geoserver', country /*malaysia*/);
else
	location_map = new GPS_Map("locationMapDiv", geoLat, geoLng, level, g_default_type);
	location_map.currentMapType = 'location';
	if(country == 'malaysia'){
		document.getElementById("lat_value").value = geo_default_lat;
		document.getElementById("lon_value").value = geo_default_lng;
	}
	
	g_geocoder = new GPS_Geocoder();
	g_marker = new GPS_Marker(new GPS_LatLng(g_default_lat, g_default_lng), {
        draggable: true
    }, location_map.map);
    location_map.addOverlay(g_marker);
    
    var c = this;
    g_marker.addListener("drag",
    function(a, b) {
        c.setLatLonLabels(b.lat(), b.lng());
    });
    g_marker.addListener("click",
    function(a, b) {
        a.openInfoWindowHtml("<table><tr><td align=left> Current position:<br><br>" + "Lat:" + a.getPoint().lat() + "  Lng:" + a.getPoint().lng()) + "</td></tr></table>";
    });
//    setPoints();
};

var testline = {};
function setTestPoints(){//  for test
//	for(var i=0; i < 10;i++){
	testline.name = "testline";
	testline.points=[];
		var point = new GPS_LatLng(parseFloat(1.54874), parseFloat(110.34332));
		testline.points.push(point);
		point = new GPS_LatLng(parseFloat(1.55874), parseFloat(110.34332));
		testline.points.push(point);
		point = new GPS_LatLng(parseFloat(1.55874), parseFloat(110.34632));
		testline.points.push(point);
//	}
}

function updateAddress(e) {
	
	get_address(e.lat(), e.lng(), function(lat, lng, address){
		document.getElementById("location_info_address").innerHTML = address;
	});
	
    /*g_geocoder.getLocations(c,
    function(a, b) {
        document.getElementById("location_info_address").innerHTML = b;
    });*/
};
function setLatLonLabels(a, b) {
    document.getElementById('lat_value').value = Math.round(a * Math.pow(10, 5)) / Math.pow(10, 5);
    document.getElementById('lon_value').value = Math.round(b * Math.pow(10, 5)) / Math.pow(10, 5);
    document.getElementById("location_info_address").innerHTML = "&nbsp;";
};

function setLocationLabelForTracker(a, b){
	document.getElementById('lat_value').value = a;
    document.getElementById('lon_value').value = b;
    document.getElementById("location_info_address").innerHTML = "&nbsp;";
};



function setMapPosition(lat_value, lon_value) {
    if (location_map == null) {
        alert("Can't find Google map.");
        return;
    }
    
	
	lat_value = lat_value.toUpperCase();
	var lat_value_sign = 1;
	if(lat_value.substring(lat_value.length - 1, lat_value.length) == 'S'){
		lat_value_sign = -1;
	}
	if(lat_value.indexOf("-") == 0){
		lat_value_sign = -1;
		lat_value = lat_value.substring(1);
	}
	
	
var lon_value_sign = 1;
lon_value = lon_value.toUpperCase();
	if(lon_value.substring(lon_value.length - 1, lon_value.length) == 'W'){
		lon_value_sign = -1;
	}	
	if(lat_value.indexOf("-") == 0){
		lon_value_sign = -1;
		lat_value = lat_value.substring(1);
	}
	
	
    lat_value = lat_value.replace(/^\s*/g, "").replace(/\s*$/g, "").replace(/[swneSWNE]$/,'');
    lon_value = lon_value.replace(/^\s*/g, "").replace(/\s*$/g, "").replace(/[swneSWNE]$/,'');
    var c = lat_value.toString().split(" ");
    var d = lon_value.toString().split(" ");
    var e = 0;
    var f = 0;
    if (c != null) {
        if (c.length == 3) {
            var g = 1;
            var h = 1;
            if (Number(c[0]) < 0) {
                g = -1;
                c[0] = Number(c[0]) * -1;
            }
            if (Number(d[0]) < 0) {
                h = -1;
                d[0] = Number(d[0]) * -1;
            }
            e = Number(c[0]) + Number(c[1]) / 60 + Number(c[2]) / 3600;
            f = Number(d[0]) + Number(d[1]) / 60 + Number(d[2]) / 3600;
            e *= g;
            f *= h;
        } else{ 
        	if(lat_value.indexOf(".") > 2){
        		var lat_d = lat_value.substr(0, 2);
            	var lat_m = lat_value.substr(2);
            	
            	e = parseFloat(parseFloat(lat_d) + parseFloat(lat_m) / 60);
            	if(isNaN(e)){
            		e = 0;
            	}
            	
            	var lng_d = lon_value.substr(0, 3);
                var lng_m = lon_value.substr(3);
                f = parseFloat(parseFloat(lng_d) + parseFloat(lng_m) / 60);
                if(isNaN(f)){
            		f = 0;
            	}
        	}
            else if (!isNaN(lat_value) && !isNaN(lon_value)) {
	            e = Number(lat_value);
	            f = Number(lon_value);
        	}
        }
    }
    //alert((lat_value_sign*e)+","+(lon_value_sign*f));
    if (e == 0 || f == 0) {
        alert(LG_Format_Error);
        return;
    }
    var i = new GPS_LatLng(lat_value_sign*e, lon_value_sign*f);
    location_map.panTo(i);
    g_marker.setPoint(i);
    updateAddress(i);
//    location_map.geoLineLayerRef.addLine(testline);
};